layui.use(['layer', 'form','element'], function(){
    var element = layui.element;
    var layer = layui.layer;
    var form = layui.form;


    //获取注册验证码
    $('#sendCaptchaButton').on('click',function () {
        var toMail = $('#r_email').val();
        var params = {
            'toMail': toMail
        };
        if(!checkIsNullOrEmpty(toMail)) {
            layer.msg('请输入邮箱地址', {icon: 5, time: 2000});
            return false;
        }
        if(!validatorEmail(toMail)) {
            layer.msg('请输入正确格式的邮箱', {icon: 5, time: 2000});
            return false;
        }
        var resFlag = isExists(toMail,null);
        if(!resFlag) {
            layer.msg('该邮箱账号不存在，请注册', {icon: 5, time: 2000});
            return false;
        }
        layer.load(2);
        $.ajax({
            url: '/user/sendMailCode',
            type: 'POST',
            contentType: 'application/json; charset=UTF-8',// 解决415错误
            dataType: 'JSON',
            data: JSON.stringify(params),
            success: function(res) {
                if(res.code == '200') {
                    layer.msg(res.msg, {icon: 1, time: 3000});
                }else { //验证码发送失败
                    layer.closeAll('loading');
                    layer.msg(res.msg, {icon: 5, time: 3000});
                }
            },
            complete: function() {
                layer.closeAll('loading');
            },
            error: function(error) {
                layer.closeAll('loading');
                layer.msg(error.msg, {icon: 5, time: 3000});
            }
        });
    });

    //自定义验证规则
    form.verify({
        userEmail: function(value){
            if(!checkIsNullOrEmpty(value)) {
                return '请输入邮箱账号';
            }
            if(!validatorEmail(value)) {
                return '请输入正确格式的邮箱账号';
            }
            var resFlag = isExists(value,null);
            if(!resFlag) {
                return "该邮箱账号不存在，请注册";
            }
        },
        userPass: function (value) {
            if(!checkIsNullOrEmpty(value)) {
                return '请输入密码';
            }
            if(!validPass(value)) {
                return "密码至少包含 数字和英文，长度6-20";
            }
        }
    });

    //监听注册提交
    form.on('submit(login-forget)',function (obj) {
        var params = {
            'userEmail': $('#r_email').val(),
            'passWord': $('#new_pass').val(),
            'verifyCode': $('#emailCode').val()
        };
        layer.load(2);
        $.ajax({
            url: '/user/updatePassWord',
            type: 'POST',
            contentType: 'application/json; charset=UTF-8',// 解决415错误
            dataType: 'JSON',
            data: JSON.stringify(params),
            success: function(res) {
                if(res.code == '200') {
                    layer.msg(res.msg, {icon: 1, time: 2000}, function () {
                        parent.layer.closeAll();
                    });
                }else { //密码修改失败
                    layer.closeAll('loading');
                    layer.msg(res.msg, {icon: 5, time: 3000});
                }
            },
            complete: function() {
                layer.closeAll('loading');
            },
            error: function(error) {
                layer.closeAll('loading');
                layer.msg(error.msg, {icon: 5, time: 3000});
            }
        });
        return false;
    });

    //校验邮箱或者昵称是否存在
    function isExists(userEmail,userNick) {
        var params = {
            'userEmail': userEmail,
            'userNick': userNick
        };
        var resFlag;
        layer.load(2);
        $.ajax({
            url: '/user/isExists',
            type: 'POST',
            contentType: 'application/json; charset=UTF-8',// 解决415错误
            dataType: 'JSON',
            data: JSON.stringify(params),
            async: false, //同步
            success: function(res) {
                if(res.code == '200') {
                    resFlag = res.data;
                    return resFlag;
                }
            },
            complete: function() {
                layer.closeAll('loading');
            },
            error: function(error) {
                layer.closeAll('loading');
                layer.msg(error.msg, {icon: 5, time: 3000});
            }
        });
        return resFlag;
    }
});